<?php

class Home extends Controller
{
   public function index()
   {
	    $comments = $this->model->getAllComments();	
		$digitalContents = $this->model->getAllDigitalContent();
		$products = $this->model->getAllProducts();		
		//print "<pre>" . print_r($categories , true ) . "<pre>"; exit;
		$aprovedproducts = $this->model->showaprovedproduct();
		$shows = $this->model->getAllSlideshows();
		//$news = $this->model->getAllBreakingNews();
		//print "<pre>" . print_r($news , true ) . "<pre>"; exit;
	    	
        // load views
        require APP . 'views/site_templates/header.php';
        require APP . 'views/home/index.php';
        require APP . 'views/site_templates/footer.php';
    }
    
	public function category($category_id)
    {
		$digitalContents = $this->model->getAllDigitalContent($category_id);
		$products = $this->model->getAllProducts($category_id);
		
		//print "<pre>" . print_r($categories , true ) . "<pre>"; exit;
	    	
        // load views
        require APP . 'views/site_templates/header.php';
        require APP . 'views/home/index.php';
        require APP . 'views/site_templates/footer.php';
    }
    
	
    public function ajaxAddToBasket($product_id) {
    	
    	// Check if user is logged in
    	if(!isset($_SESSION['user']['user_id'])) {
    		print "Please login in order to purchse items";
    		return;
    	} else {
    		$result = $this->model->addProductToBasket($product_id,$_SESSION['user']['user_id']);
    		if($result == 1) { 
    			print "Product Item Added to basket Successfully";
    		} else {
    			print "Failed to add Product Item to basket";
    		}
    	}
    }
	
	
	public function addComment()
    {
    //	Helper::verifyAdminLoggedIn();
		//print "<pre>" . print_r($_POST , true ) . "<pre>";
		//print_r($_POST);
		//die;
        if (isset($_POST["submit_comment"])) {
                $this->model->addComment($_POST["user_id"],$_POST["digital_content_id"],$_POST["comment"],$_POST["status"]);	
				
        }
        header('location: ' . URL . 'home');
    }
   
 
	public function ajaxVoteProduct($product_id) {
    	
    	// Check if user is logged in
    	if(!isset($_SESSION['user']['user_id'])) {
    		print "Please login in order to add your vote";
    		return;
    	} else {
    		$result = $this->model->productVote($product_id,$_SESSION['user']['user_id']);
    		if($result == 1) { 
    			print "Your Vote is Added Successfully";
    		} else {
    			print "Failed to Register Your Vote";
    		}
    	}
    }
    
	public function viewBasket()
    {
    	$user_id = $_SESSION['user']['user_id'];
		$basketItems = $this->model->getBasketItems($user_id);
		
		//print "<pre>" . print_r($basketItems , true ) . "<pre>"; exit;
	    	
        // load views
        require APP . 'views/site_templates/header.php';
        require APP . 'views/home/viewBasket.php';
        require APP . 'views/site_templates/footer.php';
    }

	public function checkout($amount="")
    {
    	if(empty($amount)) {
    		if($_POST['totalAmount'])
    			$amount = $_POST['totalAmount'];
    	}
    	$user_id = $_SESSION['user']['user_id'];
    	$results = 0;
    	if(isset($_POST['submit_payment']) && $_POST['submit_payment'] == 1) {
    		$validCard = $this->model->validateCreditCard($_POST['card-holder-nam'],$_POST['card-number'],$_POST['expiry-month'],$_POST['expiry-year'],$_POST['cvv']);
    		if($validCard) { 
    			$results = $this->model->processPayment($user_id,$_POST['totalAmount'],$_POST['card-holder-nam'],$_POST['card-number'],$_POST['expiry-month'],$_POST['expiry-year'],$_POST['cvv']);
    			//$results = 1;
    		} else {
    			$results = -1;
    		}
    	}
		
		//print "<pre>" . print_r($basketItems , true ) . "<pre>"; exit;
	    	
        // load views
        require APP . 'views/site_templates/header.php';
        require APP . 'views/home/checkout.php';
        require APP . 'views/site_templates/footer.php';
    }

}
